<?php
namespace app\agent\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Login extends Controller
{
    public function index()
    {
        return view('Login/login');
    }

    public function dologin(Request $request)
    {
        $post=$request->post();
        $post=myarr($post);
        if($post['phonenumber'] == null ) return "<script>alert('账号不能为空');window.location.href = document.referrer;</script>";
        if(strtolower($post['yzcode'])!=strtolower(session('yzcode'))){
            return "<script>alert('验证码错误');window.location.href = document.referrer;</script>";
        }
        $res=Db::name('agents')->where(['usernumber|phonenumber'=>$post['phonenumber']])->field('id,status,name,password,phonenumber')->find();
        if($res['password']!=Md5('lc'.$post['password'])) return "<script>alert('账号或密码错误');window.location.href = document.referrer;</script>";
        if($res['status']!=0) return "<script>alert('账号异常请联系客服');window.location.href = document.referrer;</script>";
        Session::set('agents',$res);
        if($res['phonenumber']==null){
            $data = [
                'wid'=>$res['id'],
                'logintoip' => $_SERVER['REMOTE_ADDR'],
                'logintime' => date('Y-m-d H:i:s'),
                'operation' => 'login',
                'va'=>2,//代理商
            ];
            Db::name('login_va')->insert($data);
            return "<script>alert('登录成功,请设置您的手机号');window.location.href='".url('agent/Index/index')."'</script>";
        }else{
            $data = [
                'wid'=>$res['id'],
                'logintoip' => $_SERVER['REMOTE_ADDR'],
                'logintime' => date('Y-m-d H:i:s'),
                'operation' => 'login',
                'va'=>2,//代理商
            ];
            Db::name('login_va')->insert($data);
            return "<script>alert('登录成功');window.location.href='".url('agent/Index/index')."'</script>";
        }


    }

    public function exitlogin()
    {
        $data = [
            'wid'=>session('agents.id'),
            'logintoip' => $_SERVER['REMOTE_ADDR'],
            'logintime' => date('Y-m-d H:i:s'),
            'operation' => '退出登录',
            'va'=>2,//代理商
        ];
        Db::name('login_va')->insert($data);
        Session::clear();
        $this->redirect('agent/Login/index');
    }

    public function yzm()
    {
        require_once EXTEND_PATH.'Validate/Validate.php';
        $validate=new \Validate();
        $c = $validate->code;
        session('yzcode',$c);
        $validate->doimg();

    }

    public function setLogin(){
        if(empty($_POST)){
            return view('Login/setlogin');
        }else{
            $param = $this->request->post();
            if($param['newPassword01']!=$param['newPassword02']) return "<script>alert('提交的密码不一致');history.go(-1)</script>";
            $agent = Db::name('agents')->where('phonenumber',$param['phonenumber'])->find();
            if($agent){
                $code = Db::name('user_code')->where(['mobile'=>$param['phonenumber'],'state'=>0])->order('id desc')->find();
                if($code['code']==$param['code']){
                    if(time()>=$code['reg_time']+1800){
                        return "<script>alert('验证码超时');history.go(-1)</script>";
                    }else{
                        $pwd = Md5('lc'.$param['newPassword01']);
                        if($agent['password']==$pwd) return "<script>alert('新密码不能和原密码相同');history.go(-1)</script>";
                        $up = Db::name('agents')->where('id',$agent['id'])->update(['password'=>$pwd]);
                        Db::name('user_code')->where('id',$code['id'])->update(['state'=>1]);
                        if($up){
                            return $this->success('密码找回成功，请去登录页面登录','agent/Login/index');
                        }
                    }
                }else{
                    return "<script>alert('验证码验证错误');history.go(-1)</script>";
                }
            }else{
                return "<script>alert('用户不存在');history.go(-1)</script>";
            }
        }

    }

    public function registersendMsg(){
        $mobile = $_POST['mobile'];
        $agent = Db::name('agents')->where('phonenumber',$mobile)->find();
        if(!$agent) return ['msg'=>'手机号不存在'];
        //一分钟只能发生一次
        $usercode = Db::name('user_code')->where('mobile',$mobile)->order('id desc')->find();
        if($usercode['reg_time']+60 >= time()) return ['msg'=>'一分钟只能发送一次'];
        $tplParam = rand(1000,9999);
        $tpCode = 'SMS_134815251';//支付密码添加
        $message = sendMsg($mobile,$tpCode,$tplParam);
        if($message['Message']=='OK'){
            $data = [
                'mobile' => $mobile,
                'code'   => $tplParam,
                'reg_time'=> time(),
            ];
            $code = Db::name('user_code')->insert($data);
            if($code){
                return ['msg'=>'success'];
            }else{
                return ['msg'=>'验证码发送错误'];
            }
        }else{
            return ['msg'=>'验证码发送错误'];
        }

    }
}
